package cumeo89.chii.client.ui;

import java.util.ArrayList;
import java.util.List;

public class HeaderPanelGroup {

	private List<HeaderPanel> panels = new ArrayList<HeaderPanel>();
	private HeaderPanel expandedPanel = null;

	void setExpanded(HeaderPanel p, boolean b) {
		if (b && p != expandedPanel) {
			if (expandedPanel != null) {
				expandedPanel.internalSetExpanded(false);
			}
			expandedPanel = p;
		} else if (!b && p == expandedPanel) {
			expandedPanel = null;
		}
		p.internalSetExpanded(b);
	}

	public HeaderPanel[] getPanels() {
		return (HeaderPanel[]) panels.toArray();
	}

	public boolean add(HeaderPanel p) {
		if (panels.contains(p)) {
			return false;
		}
		p.setGroup(this);
		if (p.isExpanded()) {
			if (expandedPanel != null) {
				expandedPanel.internalSetExpanded(false);
			}
			expandedPanel = p;
		}
		return panels.add(p);
	}

	public boolean remove(HeaderPanel p) {
		if (p.getGroup() == this) {
			p.setGroup(null);
		}
		return panels.remove(p);
	}

}
